package org.wxy.sdk.base.impl;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.tbruyelle.rxpermissions.RxPermissions;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.wxy.sdk.base.IBasePresenter;
import org.wxy.sdk.base.IBaseView;
import org.wxy.sdk.util.ContextUtil;
import org.wxy.sdk.util.DialogFactory;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by wxy on 2018/05/27.
 *
 * @author wxy
 * @date on 2018/05/27 11:32
 */
public abstract class BaseActivity extends AppCompatActivity implements IBaseView {
    protected static final int LAYOUT_EMPTY = -1;
    protected IBaseView mBaseViewImpl;
    private Unbinder mUnBinder;
    private BaseFragment mCurrentFragment;
    protected RxPermissions mRxPermissions;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
      //  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);

        mBaseViewImpl = new BaseViewImpl(this, getClass().getSimpleName());
        mRxPermissions = new RxPermissions(this);
        int layoutId = getContentViewId();
        if (layoutId != LAYOUT_EMPTY) {
            setContentView(layoutId);
        }
        mUnBinder = ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initVariables();
        initViews();
        loadData();
    }

    protected abstract int getContentViewId();
    protected abstract void initVariables();
    protected abstract void initViews();
    protected abstract void loadData();
    @Override
    public void showToast(String tip) {
        mBaseViewImpl.showToast(tip);
    }

    @Override
    public void showToast(@StringRes int strResId) {
        mBaseViewImpl.showToast(strResId);
    }

    @Override
    public void showLoadingDialog() {
        mBaseViewImpl.showLoadingDialog();
    }

    @Override
    public void showLoadingDialog(String msg) {
        mBaseViewImpl.showLoadingDialog(msg);
    }

    @Override
    public void showLoadingDialog(@StringRes int strResId) {
        mBaseViewImpl.showLoadingDialog(strResId);
    }

    @Override
    public void hideLoadingDialog() {
        mBaseViewImpl.hideLoadingDialog();
    }

    @Override
    public void showError(String message) {
        mBaseViewImpl.showError(message);
    }

    @Override
    public void showError(Throwable throwable, String message) {
        mBaseViewImpl.showError(throwable, message);
    }

    @Override
    public void showError(@StringRes int strResId) {
        mBaseViewImpl.showError(strResId);
    }

    @Override
    public void showError(String title, String message) {
        mBaseViewImpl.showError(title, message);
    }

    protected void addFragment(int containerId, BaseFragment fragment) {
        mCurrentFragment = fragment;
        if (!fragment.isAdded()) {
            this.getSupportFragmentManager().beginTransaction().add(containerId, fragment).commit();
        }else{
            replaceFragment(containerId,fragment);
        }
    }

    protected void replaceFragment(int containerId, BaseFragment fragment) {
        if (mCurrentFragment == null) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            transaction.hide(mCurrentFragment).add(containerId, fragment);
        } else {
            transaction.hide(mCurrentFragment).show(fragment);
        }
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.commit();
        mCurrentFragment = fragment;
    }

    protected void HideFragment(BaseFragment fragment) {
        if (fragment.isAdded()) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.hide(mCurrentFragment);
            transaction.hide(fragment);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            transaction.commit();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(BaseActivity baseActivity) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.destroy();
    }

    @Override
    public void destroy() {
        mBaseViewImpl.destroy();
        mUnBinder.unbind();
        mUnBinder = null;
        mBaseViewImpl = null;
        mRxPermissions = null;
        EventBus.getDefault().unregister(this);
        IBasePresenter presenter = getPresenter();
        if (presenter != null) {
            presenter.unsubscribe();
        }
        DialogFactory.getInstance().releaseProgress();
        ContextUtil.fixFocusedViewLeak(getApplication());
    }

    @Override
    public void refresh() {
    }

    @Override
    public void navigation(String path) {
        mBaseViewImpl.navigation(path);
    }

    @Override
    public void navigation(String path, Bundle bundle) {
        mBaseViewImpl.navigation(path, bundle);
    }

    @Override
    public void navigation(String path, int requestCode) {
        mBaseViewImpl.navigation(path, requestCode);
    }

    @Override
    public void navigation(String path, Bundle bundle, int requestCode) {
        mBaseViewImpl.navigation(path, bundle, requestCode);
    }
    @Override
    public String getStringRes(@StringRes int strResId, Object... objects) {
        return mBaseViewImpl.getStringRes(strResId, objects);
    }

    @Override
    public Context getContext() {
        return mBaseViewImpl.getContext();
    }

    protected void setStatusBar() {
        //5.0及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            //4.4到5.0
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        //android6.0以后可以对状态栏文字颜色和图标进行修改
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }



}
